<?php
require_once $_SERVER["DOCUMENT_ROOT"] . "/common/DbOperation.php";
$type = $_GET["type"];
$action = $_GET["action"];
$id = $_GET["id"];
if ($type == "load") {
    loadData($action, $id);
} else if ($type == "record") {
    recordData($action, $id);
} else {
    die("未知类型");
}

function getPrimaryKey($db, $tb_name)
{
    $datas = $db->tableFieldsInfo($db->getPrefix() . $tb_name);
    global $primary_key;
    foreach ($datas as $val) {
        if ($val["COLUMNKEY"] == "PRI") {
            $primary_key = $val["COLUMNNAME"];
            break;
        }
    }
    return $primary_key;
}

/**加载点击次数
 * @param $tb_name
 * @param $id
 * @return mixed
 */
function loadData($tb_name, $id)
{
    $db = DbOperation::getInstance();
    $primary_key = getPrimaryKey($db, $tb_name);
    $sql = "select click_times from " . $db->getPrefix() . "$tb_name where $primary_key=$id and is_del=0 limit 1";
    $resdatas = $db->execute($sql);
    error_log(json_encode($resdatas));
    echo $resdatas[0]["clickTimes"];
}

/**记录点击
 * @param $tb_name
 * @param $id
 */
function recordData($tb_name, $id)
{
    $db = DbOperation::getInstance();
    $primary_key = getPrimaryKey($db, $tb_name);
    $sql = "update " . $db->getPrefix() . "$tb_name set click_times=click_times+1 where $primary_key=$id";
    $db->execute($sql);
}

?>